﻿/// <reference path="TsLinq.d.ts" />
/// <reference path="Linq.ts" />
/// <reference path="DefaultComparer.ts" />

class List<T> extends Linq<T>{

    // 
    // Adds a new item to the list.
    push(item: T) {
        this._items.push(item);
    }

    //  
    //  Clears all items in the dictionary.
    //
    clear() {
        this._items = [];
    }

    //
    //  Adds all elements to the current list
    //
    addRange(items: T[]): void {
        items.forEach(i => this.push(i));
    }

    //
    //  Inserts an item on the given position. If no position is given, the item is added as the first item in the collection.
    //
    insert(item: T, position: number = 0) {
        this._items.splice(position, 0, item);
    }

    //
    //  Removes the first occurrence of a specific object. If the object was not found, an error is thrown.
    //
    remove(item: T, comp?: IComparer<T>): T {
        var index = this.indexOf(item, comp);
        if (index == -1) {
            throw new Error('Can\'t find the item to remove.');
        }
        return this.removeAt(index);
    }

    //  
    //  Removes the element on the specified position.
    //
    removeAt(position: number): T {
        if (position >= this._items.length || position < 0) {
            throw new Error('Position is not in range');
        }
        var result = this._items[position];
        this._items.splice(position, 1);
        return result;
    }
} 